package com.example.boot3.service.Impl;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.example.boot3.mapper.UseMapper;
import com.example.boot3.mapper.VehicleMapper;
import com.example.boot3.model.Return;
import com.example.boot3.model.Vehicle;
import com.example.boot3.service.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author 邹成明
 */
@Service
public class VehicleServiceImpl implements VehicleService {
    @Autowired
    VehicleMapper vehicleMapper;
    @Autowired
    UseMapper useMapper;

    @Override
    public List<Vehicle> getVehiclesInfoList() {
        List<Vehicle> result = vehicleMapper.getVehiclesInfoList();
        return result;
    }

    @Override
    public Integer addVehicle(Vehicle vehicle) {
        try {
            vehicleMapper.addVehicle(vehicle);
            return 1;
        } catch (DuplicateKeyException e) {
            // 捕获唯一键约束冲突的异常
            return 0;
        }
    }

    @Override
    public List<Map<String, String>> getVehicleCountByFleet() {
        List<Map<String, String>> result = vehicleMapper.getVehicleCountByFleet();
        return result;
    }

    @Override
    public List<Map<String, String>> getVehicleCountByStatus() {
        List<Map<String, String>> result = vehicleMapper.getVehicleCountByStatus();
        return result;
    }

    @Override
    public List<Map<String, String>> getVehicleCountByType() {
        List<Map<String, String>> result = vehicleMapper.getVehicleCountByType();
        return result;
    }

    @Override
    public List<Map<String, String>> getVehicleUseTimesList() {
        List<Map<String, String>> result = vehicleMapper.getVehicleUseTimesList();
        return result;
    }

    @Override
    public List<Map<String, String>> getVehiclesMiles() {
        return vehicleMapper.getVehiclesMiles();
    }

    @Override
    public List<Map<String, String>> getVehiclesCostList() {
        return vehicleMapper.getVehicleCostList();
    }

    @Override
    public Integer returnVehicle(Return returnVehicle) {
        useMapper.updateEndDate(returnVehicle);
        return vehicleMapper.updateVehicleStatus(returnVehicle.getVehicle_id(),"空闲");
    }

    @Override
    public Integer deleteVehicle(String vehicle_id) {
        return vehicleMapper.deleteVehicle(vehicle_id);
    }

}
